<?php
namespace app\app\model;
use think\Model;
use think\Db;

class Address extends Model{

	public $user_id;
	
	public function del($id){
		//查询数据是否存在
		$have = $this->get($id);
		if( !$have ) {
			return ['code'=>402,'msg'=>'地址不存在'];
		}
		//更新
		$this->update(['id'=>$id,'status'=>0]);
		//删除我的默认地址
		$address = Db::name('user_address')->where('user_id','eq',$this->user_id)->where('addr_id','eq',$id)->find();
		try {
			Db::name('user_address')->where('id','eq',$address['id'])->update(array('is_default'=>0));
			return ['code'=>200,'msg'=>'删除成功'];	
		} catch (Exception $e) {
			throw new Exception(['code'=>402,'msg'=>'默认地址删除失败']);
		}
	}

	public function getAll(){
		$where['user_id'] = array('eq',$this->user_id);
		$where['status']  = array('eq',1);
		$data = $this->field('id,name,phone,address,details')->where($where)->select();
		return $data;		
	}

}